package com.gxa.learning.pc.service.impl;

import com.gxa.learning.core.common.ExceptionCode;
import com.gxa.learning.core.exception.BusinessException;
import com.gxa.learning.core.pojo.Role;
import com.gxa.learning.core.pojo.RoleMenuVO;
import com.gxa.learning.core.service.impl.BaseServiceImpl;
import com.gxa.learning.pc.mapper.RoleMapper;
import com.gxa.learning.pc.mapper.RoleMenuMapper;
import com.gxa.learning.pc.service.RoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author lijianmin
 * @date 2021/12/07  20:47:36
 */
@Service("roleService")
@Transactional(rollbackFor = {Throwable.class}, propagation = Propagation.REQUIRED)
public class RoleServiceImpl extends BaseServiceImpl<Role> implements RoleService {

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private RoleMenuMapper roleMenuMapper;

    /**
     * 根据条件搜索的方法
     *
     * @param roleName   角色名称
     * @param company    公司名称
     * @param department 部门名称
     * @param state      角色状态
     * @return 返回查询到的集合
     */
    @Override
    public List<RoleMenuVO> selectByLike(String roleName, String company, String department, Boolean state) {

        return roleMapper.selectByLike(roleName, company, department, state);
    }

    /**
     * 根据id删除角色并删除角色相关的记录的方法
     *
     * @param id 删除的角色ID
     * @return 返回消息
     * @throws BusinessException 操作中产生的异常
     */
    @Override
    public Boolean deleteByRoleId(Long id) throws BusinessException {
        //删除角色
        Integer integer = roleMapper.deleteById(id);
        //删除角色权限
        Integer integer1 = roleMenuMapper.deleteByRoleId(id);
        if (integer <= 0 || integer1 <= 0) {
            throw new BusinessException(ExceptionCode.NO_SUCH_OBJ);
        }
        return true;
    }

    /**
     * 添加角色的方法
     *
     * @param map 添加的字段
     * @return 返回消息
     * @throws BusinessException 操作中产生的异常
     */
    @Override
    public Boolean add(Map<String, Object> map) throws BusinessException {
        // 必传字段进行判断
        if (map.get("mark") == null) {
            throw new BusinessException(ExceptionCode.ARGUMENTS_ERROR);
        }
        // 获取map中的role的字段并赋值给role
        Role role = new Role();
        role.setId(map.get("id") != null ? Long.valueOf(map.get("id").toString()) : null);
        role.setName(map.get("name") != null ? map.get("name").toString() : null);
        role.setMark(map.get("mark").toString());
        role.setState(true);
        role.setCompany(map.get("company") != null ? map.get("company").toString() : null);
        role.setDepartment(map.get("department") != null ? map.get("department").toString() : null);
        Integer roleAll = roleMapper.add(role);

        Long roleId = role.getId();
        //获取角色对应的菜单数组
        List<Object> menuIds = (List) map.get("menuIds");
        //遍历权限数组
        for (Object menuId : menuIds) {
            //添加角色对应的菜单
            Integer menu = roleMenuMapper.add(map.get("id") != null ? Long.valueOf(map.get("id").toString()) : roleId, Long.valueOf(menuId.toString()));
            //如果某一次失败 则直接抛异常 回滚
            if (menu <= 0) {
                throw new BusinessException(ExceptionCode.NOT_ID_ERROR);
            }
        }
        return roleAll > 0;
    }

    /**
     * 根据id修改角色状态的方法
     *
     * @param id 角色id
     * @return 返回是否成功
     * @throws BusinessException 操作中产生的异常
     */
    @Override
    public Boolean updateState(Long id) throws BusinessException {
        Role role = super.selectById(id);
        if (role != null) {
            if (role.getId() == null) {
                throw new BusinessException(ExceptionCode.NO_SUCH_OBJ);
            }
        } else {
            throw new BusinessException(ExceptionCode.NO_SUCH_OBJ);
        }
        //获取查询到的状态 并取反
        Boolean state = !role.getState();

        Integer integer = roleMapper.updateState(state, id);

        return integer > 0;
    }

}
